package com.ruoyi.register.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.register.entily.User;
import com.ruoyi.register.mapper.RegisterMapper;
import com.ruoyi.register.result.Result;
import com.ruoyi.register.service.RegisterService;
import com.ruoyi.register.utils.AllAuthentication;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
public class RegisterServiceImpl implements RegisterService {

    @Resource
    RegisterMapper registerMapper;

    @Override
    public Result insert(User user) {
        //先查询用户是否存在
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().ge(User::getUname,user.getUname());
        User tbUser1 = registerMapper.selectOne(queryWrapper);
        if(tbUser1!=null){
            return new Result(false,0,"姓名已经存在，不能注册",null);
        }
        //用户名不存在
        //加密
        //认证身份
        if(user.getIdcard().length()>0) {
            Boolean aBoolean = AllAuthentication.IdCardAuthentication(user);
            if (aBoolean = false) {
                return new Result(true, 0, "身份证信息和名字不匹配", null);
            }
        }
        if(user.getBankcard().length()>0) {
            AllAuthentication.CardAuthentication(user.getBankcard(),user.getIdcard(),user.getUname());
            BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
            String encode = encoder.encode(user.getPwd());
            user.setPwd(encode);
            registerMapper.insert(user);
            return new Result(true,1,"注测成功",null);
        }
        return new Result(false, 0, "银行卡不正确", null);
        }

}
